/*
 *  PhrasePath.h
 *  OfflineTranslator
 *
 *  Created by PH on 5.9.10.
 *  Copyright 2010 Petr Homola. All rights reserved.
 *
 */

#ifndef NLP_PHRASEPATH_H_
#define NLP_PHRASEPATH_H_

#include "global.h"
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "Phrase.h"

namespace nlp {
	
	using namespace std;
#ifdef TR1
	using namespace std::tr1;
#endif
	
	class PhrasePath {
		
		private:
		Phrase* phrase;
		PhrasePath* tail;

		public:
		PhrasePath(Phrase* p, PhrasePath* t) {
			phrase = p;
			tail = t;
			//cout << "Path: 1 -- " << description() << endl;
		}
		
		/*~PhrasePath() {
			cout << "~Path: 2 -- " << description() << endl;
		}*/
		
		int getPenalty() const {
			__block int penalty = 0;
			enumeratePhrases(BLOCK(&penalty)(Phrase* phrase) {
				penalty += phrase->getPenalty();
			});
			return penalty;
		}
		
		int getLength() const {
			return (tail == NULL ? 1 : tail->getLength() + 1) + (phrase->isUsed() ? 1 : 0);
		}
        
        Phrase* getPhrase() const { return phrase; }
        
        PhrasePath* getTail() const { return tail; }
		
		void enumeratePhrases(BLOCKVAR(block, void, Phrase*)) const {
			block(phrase);
			if (tail != NULL) tail->enumeratePhrases(block);
		}
		
		void linearize(BLOCKVAR(block, void, const string&)) const {
			linearize("", block);
			//block(description());
		}
		
		void linearize(const string prefix, BLOCKVAR(block, void, const string&)) const {
			phrase->linearize(prefix, tail, block);
		}
		
		string description() const {
			return tail == NULL ? phrase->description() : phrase->description() + " " + tail->description();
		}

	};

}

#endif
